#include <time.h>
#include "task.hpp"
// 生产者
void *producer(void *args) {
    blocking_queue<task<int>> *q = static_cast<blocking_queue<task<int>> *>(args); 
    // 生产数据
    while(true) {
        int e1 = rand() % 10, e2 = rand() % 10;
        cout << "生产者生产了数据: e1 = " << e1 << ", e2: " << e2 << endl;
        task<int> t(e1, e2);
        q->push(t);
    }
}

// 消费者
void *consumer(void *args) {
    blocking_queue<task<int>> *q = static_cast<blocking_queue<task<int>> *>(args); 
    // 消费数据
    while(true) {
        task<int> t = q->pop();
        pair<int, int> pii = t.get_args();
        int ans = t.run();
        cout << "消费者消费了数据: e1 = " << pii.first << ", e2: " << pii.second << " = " << ans << endl;
    }
}


int main() {
    blocking_queue<task<int>> *q = new blocking_queue<task<int>>;
    srand(time(nullptr));
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, q);
    pthread_create(&p, nullptr, producer, q);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    return 0;
}